﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlxRRSReporter.Utils;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using GlxRRSDB.Data;
using GlxRRSDB.Utils;


namespace GlxRRSReporter.DailyReporter
{
    public class WeeklyReporterByFilm
    {
        private const int NGUYEN_DU_CINEMA_ID = 1001;
        private const int NGUYEN_TRAI_CINEMA_ID = 1002;
        private const int TAN_BINH_CINEMA_ID = 1003;
        //
        private const string SHEET_PER_CINEMA = "WR by Film";
        private const string SHEET_3 = "Sheet3";
        private static string TEMPLATE_NAME = ".\\templates\\WeeklyReportByFilmTemplate.xls";
        private static string OUT_MONTHLY_NAME = ".\\templates\\WeeklyReportByFilmTemplate.xls";

        public static void init(string template, string outNameVal)
        {
            TEMPLATE_NAME = template;
            OUT_MONTHLY_NAME = outNameVal;
        }
        public static void generateWeeklyReporterByFilm(DateTime current,
            Dictionary<int, Dictionary<int, WeeklyReportData>> dictDetailFilm)
        {
            DateTime startOfWeek = current.StartOfWeek(DayOfWeek.Friday);
            // load excel file
            Excel.Workbook workbook = ExportToExcel.openExcelWorkbooks(TEMPLATE_NAME);
            Excel.Worksheet sheet = workbook.Worksheets[SHEET_PER_CINEMA];
            // title
            sheet.get_Range("B5").Formula = startOfWeek.Date.ToString();
            //
            string[] column = new string[] { "D", "E", "F", "G", "H", "I" };
            string[] column1001 = new string[] { "D", "E" };
            string[] column1002 = new string[] { "F", "G"};
            string[] column1003 = new string[] { "H", "I" };
            string[] row = new string[] { "7", "8", "9", "10", "11", "12", "13", "14" };
            #region CHART Film Admission DETAIL
            int columnIdx = -1;
            int rowIdx = -1;
            foreach (Dictionary<int, WeeklyReportData> dict in dictDetailFilm.Values)
            {
                columnIdx = 0;
                rowIdx++;
                foreach (WeeklyReportData detail in dict.Values)
                {
                    if (detail.CinemaID == NGUYEN_DU_CINEMA_ID)
                    {
                        column = column1001;
                    }
                    else if (detail.CinemaID == NGUYEN_TRAI_CINEMA_ID)
                    {
                        column = column1002;
                    }
                    else if (detail.CinemaID == TAN_BINH_CINEMA_ID)
                    {
                        column = column1003;
                    }
                    // film title
                    sheet.get_Range("B" + row[rowIdx]).Formula = detail.FilmTitle;
                    // week play
                    sheet.get_Range("C" + row[rowIdx]).Formula = detail.WeeksInPlay;
                    //
                    string addr = column[columnIdx] + row[rowIdx];
                    sheet.get_Range(addr).Formula = detail.SumWeekAdmission;
                    // bo
                    addr = column[columnIdx+1] + row[rowIdx];
                    sheet.get_Range(addr).Formula = detail.SumWeekBO;
                }
            }
            #endregion
            // gen file name
            workbook.SaveAs(OUT_MONTHLY_NAME);
        }
    }
}
